package com.lumberer.pay2021.wx.init.config;

import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.lumberer.pay2021.wx.init.loading.CertificateLoad;

/**
 * 商户信息配置初始~读取配置文件或者进行初始化
 * @author lumberer
 * @date 2021年9月23日
 */
@Component
public class MerchantConfig{
	/**
	 * ="wx2741af0c1d6587ff"
	 */
	public static String appid;
	/**
	 * 商户号
	 *="1613565102"
	 */
	public static String mchid;
	/**
	 * APIV3密钥
	 * ="cEv1h9RBfkVvuvT3HKxtgjHNpAf3NWih"
	 */
	public static String apiV3;
	/**
	 * 密钥路径
	 * ="C:/Users/luoqy/Desktop/中云项目/项目资料/贞丰人民医院/apiclient_key.pem"
	 */
	public static String keyPath;
	//微信平台证书
	//request.getHeader("wechatpay")
	//="C:/Users/luoqy/Desktop/中云项目/项目资料/贞丰人民医院/wechatpay_33110535817EE149C6C4D0BADF8B05A31A57670B.pem"
	public static String wechatpayPath;
	//API证书编号cert
	//request.getHeader("serialNumber")
	//="66697723CA20AA229AF7C78ADC321B9887CE782A"
	
	public static String serialNumber;
	 // 定义全局容器 保存微信平台证书公钥  注意线程安全
    public static Map<String, X509Certificate> certificateMap = new ConcurrentHashMap<>();
    
    
    public String getAppid() {
		return appid;
	}

    @Value("${MerchantConfig.appid}")
	public  void setAppid(String appid) {
		MerchantConfig.appid = appid;
	}


	public static String getMchid() {
		return mchid;
	}

	@Value("${MerchantConfig.mchid}")
	public  void setMchid(String mchid) {
		MerchantConfig.mchid = mchid;
	}


	public  String getApiV3() {
		return apiV3;
	}

	@Value("${MerchantConfig.apiV3}")
	public  void setApiV3(String apiV3) {
		MerchantConfig.apiV3 = apiV3;
	}


	public  String getKeyPath() {
		return keyPath;
	}

	@Value("${MerchantConfig.keyPath}")
	public  void setKeyPath(String keyPath) {
		MerchantConfig.keyPath = keyPath;
	}


	public  String getWechatpayPath() {
		return wechatpayPath;
	}

	@Value("${MerchantConfig.wechatpayPath}")
	public  void setWechatpayPath(String wechatpayPath) {
		MerchantConfig.wechatpayPath = wechatpayPath;
	}


	public  String getSerialNumber() {
		return serialNumber;
	}

	@Value("${MerchantConfig.serialNumber}")
	public  void setSerialNumber(String serialNumber) {
		MerchantConfig.serialNumber = serialNumber;
	}


	public static Map<String, X509Certificate> getCertificateMap() {
		return certificateMap;
	}


	public static void setCertificateMap(Map<String, X509Certificate> certificateMap) {
		MerchantConfig.certificateMap = certificateMap;
	}


	/**
     * 初始化商家信息配置
     * @param mchid 商户号
     * @param apiV3 APIV3KEY密钥
     * @param keyPath 商家密钥路径
     * @param wechatpayPath 微信平台证书
     * @param serialNumber 商家安全平台编号
     */
	public static void init(String appid,String mchid,String apiV3,String keyPath,String wechatpayPath,String serialNumber){
		if(!"".equals(appid)&&null!=appid){
			MerchantConfig.appid=appid;
		}
		if(!"".equals(mchid)&&null!=mchid){
			MerchantConfig.mchid=mchid;
		}
		if(!"".equals(apiV3)&&null!=apiV3){
			MerchantConfig.apiV3=apiV3;
		}
		if(!"".equals(keyPath)&&null!=keyPath){
			MerchantConfig.keyPath=keyPath;
		}
		if(!"".equals(wechatpayPath)&&null!=wechatpayPath){
			MerchantConfig.wechatpayPath=wechatpayPath;
		}
		if(!"".equals(serialNumber)&&null!=serialNumber){
			MerchantConfig.serialNumber=serialNumber;
		}
		Map<String, X509Certificate> certificateMap=CertificateLoad.refreshCertificate();
		MerchantConfig.certificateMap.putAll(certificateMap);
	}
	
}
